package com.tydic.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by maliang on 2017/5/17.
 */
public class NumberUtil {
    private static  final Logger logger = LoggerFactory.getLogger(NumberUtil.class);

    public static Double maxCountOfArray(ArrayList<Double> array){
        // map的key存放数组中存在的数字，value存放该数字在数组中出现的次数
        HashMap<Double, Integer> map = new HashMap<Double, Integer>();
        for (int i = 0; i < array.size(); i++) {
            if (map.containsKey(array.get(i))) {
                int temp = map.get(array.get(i));

                map.put(array.get(i), temp + 1);
            } else {
                map.put(array.get(i), 1);
            }
        }
        Collection<Integer> count = map.values();
        // 找出map的value中最大的数字，也就是数组中数字出现最多的次数
        int maxCount = Collections.max(count);
        Double maxNumber = 0d;
        for (Map.Entry<Double, Integer> entry : map.entrySet()) {
            // 得到value为maxCount的key，也就是数组中出现次数最多的数字
            if (maxCount == entry.getValue()) {
                maxNumber = entry.getKey();
            }
        }
        logger.info("出现次数最多的数字为：" + maxNumber);
        logger.info("该数字一共出现" + maxCount + "次");
        return maxNumber;
    }
}
